new Vue({
    el:'#app',
    data:{
        buses: [
            {"id": "K1", "startStation": "四惠枢纽站", "stops": "八王坟西,郎家园,大北窑东,大北窑西", "arrivalStation": "德茂庄", "capacity": 25, "departureTime": "首末班: 05:00-23:00", "frequency": "5分钟一趟"},
            {"id": "K1", "startStation": "四惠枢纽站", "stops": "八王坟西,郎家园,大北窑东,大北窑西", "arrivalStation": "德茂庄", "capacity": 25, "departureTime": "首末班: 05:00-23:00", "frequency": "5分钟一趟"}, 
            {"id": "K1", "startStation": "四惠枢纽站", "stops": "八王坟西,郎家园,大北窑东,大北窑西", "arrivalStation": "德茂庄", "capacity": 25, "departureTime": "首末班: 05:00-23:00", "frequency": "5分钟一趟"},  
        ],
        search_buses: [],
        keyword: "",
        showform: false,
        id: "",
        startStation: "",
        stops: "",
        arrivalStation: "",
        departureTime: "",
        frequency: "",
        capacity: 1,
        edit_index: 0,
        update_type: 0,
        notsearch: true,
        search_edit_index: 0,
        search_mode: "id",
        url: "127.0.0.1:6001",
    },
    created(){
        // 像后端发送请求

        
        var that = this
        axios({
            method: "get",
            url: "http://" + that.url + "/product/all",
        }).then(function(response) {
            let result = response.data
            console.log(result)
            if (result.code == 200) {
                that.buses = result.data
            }
        })
        
        

    },
    mounted(){

    },
    watch:{
        
    },
    methods:{
        trim:function (ele){
            if(typeof ele === 'string'){
               return ele.split(' ').join('');
            }else{
                console.error(`${typeof ele} is not the expected type, but the string type is expected`)
            }
        },
        search:function () {
            console.log(this.keyword)
            keyword = this.trim(this.keyword)
            console.log(keyword)
            console.log(this.search_mode)
            var find = 0
            this.search_buses = []

            for (var index = 0; index < this.buses.length; index++) {

                if (this.search_mode == "id") {
                    if (this.buses[index].id == keyword.toUpperCase()) {
                        console.log("find!")
                        find = 1
                        this.search_buses.push(this.buses[index])
                        this.search_buses[this.search_buses.length - 1].original_index = index
                        this.notsearch = false
                        
                    }
                } else if (this.search_mode == "start") {
                    if (this.buses[index].startStation == keyword.toUpperCase()) {
                        console.log("find!")
                        find = 1
                        this.search_buses.push(this.buses[index])
                        this.search_buses[this.search_buses.length - 1].original_index = index
                        this.notsearch = false
                    }
                } else if (this.search_mode == "target") {
                    var stoparr = this.buses[index].stops.split(',')
                    stoparr.push(this.buses[index].arrivalStation)
                    console.log(stoparr)
                    for (var j = 0; j < stoparr.length; j++) {
                        if (stoparr[j] == keyword.toUpperCase()) {
                            find = 1
                            this.search_buses.push(this.buses[index])
                            this.search_buses[this.search_buses.length - 1].original_index = index
                            this.notsearch = false
                            break
                        }
                    }


                }
                



            }

            if (find == 0) {
                layer.msg("无查找结果")
            }

        },
        backtoindex:function () {
            this.notsearch = true
            this.search_buses = []
        },
        
        cancel_show:function () {
            this.showform = false
        },
        submit_update:function () {
            console.log("submit")
            if (this.id == "") {
                return
            }
            if (this.startStation == "") {
                return
            }
            if (this.stops == "") {
                return
            }
            if (this.arrivalStation == "") {
                return
            }
            if (this.departureTime == "") {
                return
            }
            if (this.frequency == "") {
                return
            }
            if (this.capacity == 0) {
                return
            }

            this.showform = false
            if (this.update_type == 0) {
                console.log("submit mode: update")
                this.buses[this.edit_index].id = this.trim(this.id)
                this.buses[this.edit_index].startStation = this.trim(this.startStation)
                this.buses[this.edit_index].stops = this.trim(this.stops)
                this.buses[this.edit_index].arrivalStation = this.trim(this.arrivalStation)
                this.buses[this.edit_index].departureTime = this.trim(this.departureTime)
                this.buses[this.edit_index].frequency = this.trim(this.frequency)
                this.buses[this.edit_index].capacity = this.capacity

                if (this.search_buses.length != 0) {
                    this.search_buses[this.search_edit_index].id = this.trim(this.id)
                    this.search_buses[this.search_edit_index].startStation = this.trim(this.startStation)
                    this.search_buses[this.search_edit_index].stops = this.trim(this.stops)
                    this.search_buses[this.search_edit_index].arrivalStation = this.trim(this.arrivalStation)
                    this.search_buses[this.search_edit_index].departureTime = this.trim(this.departureTime)
                    this.search_buses[this.search_edit_index].frequency = this.trim(this.frequency)
                    this.search_buses[this.search_edit_index].capacity = this.capacity
                }

                //向后端发送一个请求，修改一个车次

                
                
                var that = this
                axios({
                    method: "put",
                    url: "http://" + that.url + "/product/update",
                    data: {
                        id: that.id,
                        startStation: that.startStation,
                        stops: that.stops,
                        arrivalStation: that.arrivalStation,
                        departureTime: that.departureTime,
                        frequency: that.frequency,
                        capacity: that.capacity
                    },
                }).then(function(response) {
                    let result = response.data
                    console.log(result)
                    if (result.code == 200) {
                        layer.msg("修改成功")
                    }
                })

                

                

            } else {
                console.log("submit mode: add")
                bus = {
                    "id": this.trim(this.id),
                    "startStation": this.trim(this.startStation),
                    "stops": this.trim(this.stops),
                    "arrivalStation": this.trim(this.arrivalStation),
                    "departureTime": this.trim(this.departureTime),
                    "frequency": this.trim(this.frequency),
                    "capacity": this.capacity
                }
                this.buses.push(bus)

                // 向后端发送请求，增加一个车次
                var that = this
                axios({
                    method: "post",
                    url: "http://" + that.url + "/product/add",
                    data:{
                        id: that.id,
                        startStation: that.startStation,
                        stops: that.stops,
                        arrivalStation: that.arrivalStation,
                        departureTime: that.departureTime,
                        frequency: that.frequency,
                        capacity: that.capacity
                    },
                }).then(function(response) {
                    let result = response.data
                    console.log(result)
                    if (result.code == 200) {
                        layer.msg("添加成功")
                    }
                })

                


            }
            

        },
        export_excel:function () {
            if (this.buses.length == 0) {
                layer.msg("暂无数据可导出")
            } else {

                
                var header = [{value:'车次'}, {value:'始发站'}, {value:'经停站'}, {value:'终点站'}, {value:'载客量'}, {value:'发车时间'}, {value:'班车频次'}]
                var data = new Array();
                data.push(header)
                for (var index = 0; index < this.buses.length; index++) {
                    var row = new Array();
                    var c1 = {type: String, value: this.buses[index].id}
                    var c2 = {type: String, value: this.buses[index].startStation}
                    var c3 = {type: String, value: this.buses[index].stops}
                    var c4 = {type: String, value: this.buses[index].arrivalStation}
                    var c5 = {type: Number, value: this.buses[index].capacity}
                    var c6 = {type: String, value: this.buses[index].departureTime}
                    var c7 = {type: String, value: this.buses[index].frequency}
                    
                    row.push(c1)
                    row.push(c2)
                    row.push(c3)
                    row.push(c4)
                    row.push(c5)
                    row.push(c6)
                    row.push(c7)
                
                    data.push(row)
                }
                writeXlsxFile(data, {
                    fileName: 'buses.xlsx'
                })
                layer.msg("导出成功")
            }
        },
        edit_bus:function (index) {
            this.showform = true
            this.update_type = 0

            this.id = this.buses[index].id
            this.startStation = this.buses[index].startStation
            this.stops = this.buses[index].stops
            this.arrivalStation = this.buses[index].arrivalStation
            this.departureTime = this.buses[index].departureTime
            this.frequency = this.buses[index].frequency
            this.capacity = this.buses[index].capacity

            this.edit_index = index
        },
        edit_search_bus:function (index) {
            this.showform = true
            this.update_type = 0
            this.search_edit_index = index

            this.id = this.search_buses[index].id
            this.startStation = this.search_buses[index].startStation
            this.stops = this.search_buses[index].stops
            this.arrivalStation = this.search_buses[index].arrivalStation
            this.departureTime = this.search_buses[index].departureTime
            this.frequency = this.search_buses[index].frequency
            this.capacity = this.search_buses[index].capacity

            this.edit_index = this.search_buses[index].original_index
        },
        import_excel:function () {
            const input = document.getElementById('inputfile')
            console.log(input)
            var that = this
            readXlsxFile(input.files[0]).then((rows) => {
                for (var index = 1; index < rows.length; index++) {
                    bus = {
                        "id": rows[index][0],
                        "startStation": rows[index][1],
                        "stops": rows[index][2],
                        "arrivalStation": rows[index][3],
                        "capacity": rows[index][4],
                        "departureTime": rows[index][5],
                        "frequency": rows[index][6]
                    }
                    this.buses.push(bus)
                    // 向后端发送请求，增加一个车次

                    
                    axios({
                        method: "post",
                        url: "http://" + that.url + "/product/add",
                        data:{
                            id: rows[index][0],
                            startStation: rows[index][1],
                            stops: rows[index][2],
                            arrivalStation: rows[index][3],
                            departureTime: rows[index][5],
                            frequency: rows[index][6],
                            capacity: rows[index][4]
                        },
                    }).then(function(response) {
                        let result = response.data
                        console.log(result)
                        if (result.code == 200) {
                            console.log("添加成功")
                        }
                    })

                }
            })
            layer.msg("导入成功")
        },
        delete_bus:function (index_) {
            console.log(index_)
            var that = this
            
            layer.open({
                content: '您确定要删除吗？'
                ,btn: ['确定', '算了吧']
                ,yes: function(index, layero){
                    console.log("确定")
                    deleted_id = that.buses[index_].id
                    that.buses.splice(index_, 1)
                    layer.closeAll();

                    //调用后端产商品中心删除API
                    
                    axios({
                        method: "delete",
                        url: "http://" + that.url + "/product/delete",
                        params:{
                            id: deleted_id,
                        },
                    }).then(function(response) {
                        let result = response.data
                        console.log(result)
                        if (result.code == 200) {
                            layer.msg("删除成功")
                        }
                    })
                    
                }
                ,btn2: function(index, layero){
                    console.log("算了")
                }
                ,cancel: function(index, layero){
                    console.log("算了")
                }
            });
        },
        delete_search_bus:function (deleted_index) {
            
            var that = this
            
            layer.open({
                content: '您确定要删除吗？'
                ,btn: ['确定', '算了吧']
                ,yes: function(index, layero){
                    console.log("确定")
                    deleted_id = that.search_buses[deleted_index].id
                    that.buses.splice(that.search_buses[deleted_index].original_index, 1)
                    that.search_buses.splice(deleted_index, 1)
                    layer.closeAll()

                    //调用后端产商品中心删除API
                    axios({
                        method: "delete",
                        url: "http://" + that.url + "/product/delete",
                        params:{
                            id: deleted_id,
                        },
                    }).then(function(response) {
                        let result = response.data
                        console.log(result)
                        if (result.code == 200) {
                            layer.msg("删除成功")
                        }
                    })
                    

                }
                ,btn2: function(index, layero){
                    console.log("算了")
                }
                ,cancel: function(index, layero){
                    console.log("算了")
                }
            });
        },
        add:function () {
            this.showform = true
            this.update_type = 1

            this.id = ""
            this.startStation = ""
            this.stops = ""
            this.arrivalStation = ""
            this.departureTime = ""
            this.frequency = ""
            this.capacity = 1

        },
        
        
    }
})
